package com.crm.service.impl.systemManager;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.customMapper.systemManager.CallManagerCustomMapper;
import com.crm.dao.mapper.systemManager.CallManagerMapper;
import com.crm.model.entity.systemManager.CallManager;
import com.crm.model.entity.systemManager.CallManagerExample;
import com.crm.model.qo.systemManager.CallManagerQO;
import com.crm.model.vo.systemManager.CallManagerVO;
import com.crm.service.systemManager.CallManagerDbService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author renshaorong
 * @Date 2021/4/20
 */
@Service
public class CallManagerDbServiceImpl implements CallManagerDbService {

    @Autowired
    private CallManagerCustomMapper callManagerCustomMapper;

    @Autowired
    private CallManagerMapper callManagerMapper;

    /**
     * 获取所有呼叫信息列表
     * @param callManagerQO
     * @param toString
     * @return
     */
    @Override
    public List<CallManagerVO> getAllCallLists(CallManagerQO callManagerQO, String toString) {
        return callManagerCustomMapper.getAllCallLists(callManagerQO,toString);
    }

    /**
     * 根据主键更新内容（属性为空不更新）
     * @param callManager
     * @return
     */
    @Override
    public boolean updateByCallIdSelective(CallManager callManager) {
        return callManagerMapper.updateByPrimaryKeySelective(callManager) > 0;
    }

    /**
     * 根据主键查找信息
     * @param callId
     * @return
     */
    @Override
    public CallManager selectByPrimaryKey(String callId){
        return callManagerMapper.selectByPrimaryKey(callId);
    }

    /**
     * 更新呼叫信息（属性为空更新）
     * @param callManager
     * @return
     */
    @Override
    public boolean updateByCallId(CallManager callManager) {
        return callManagerMapper.updateByPrimaryKey(callManager) > 0;
    }

    /**
     * 获取给定用户绑定的信息列表
     * @param accountId
     * @return
     */
    @Override
    public List<CallManager> getCallListByAccountId(String accountId){
        CallManagerExample callManagerExample = new CallManagerExample();
        CallManagerExample.Criteria criteria = callManagerExample.createCriteria();
        criteria.andAccountIdEqualTo(accountId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        return callManagerMapper.selectByExample(callManagerExample);
    }

    /**
     * 插入数据
     * @param callManager
     * @return
     */
    @Override
    public boolean insertSelective(CallManager callManager) {
        return callManagerMapper.insertSelective(callManager) > 0;
    }

    /**
     * 根据号码获取列表
     * @Date 2021/4/23
     * @param telNum
     * @return
     */
    @Override
    public List<CallManager> getCallManagerListByTelNum(String telNum){
        CallManagerExample callManagerExample = new CallManagerExample();
        CallManagerExample.Criteria criteria = callManagerExample.createCriteria();
        criteria.andTelephoneNumberEqualTo(telNum);
        return callManagerMapper.selectByExample(callManagerExample);
    }

    /**
     * 拨打电话-线路切换获取随机号码
     * @Author pym
     * @Date  2021/5/26 8:39
     * @param type 号码类型 0、中继号 1、本地号 2、全国号
     * @param companyId 公司ID
     * @return {@link {java.util.List<com.crm.model.entity.systemManager.CallManager>}}
     */
    @Override
    public List<CallManager> getRandomTelephoneNumber(String type, String companyId) {
        CallManagerExample callManagerExample = new CallManagerExample();
        CallManagerExample.Criteria criteria = callManagerExample.createCriteria();
        criteria.andAccountIdIsNull();
        criteria.andTypeEqualTo(Short.parseShort(type));
        if (StringUtils.isNotBlank(companyId)) {
            criteria.andCompanyIdEqualTo(companyId);
        }
        criteria.andUseStatusEqualTo(Short.parseShort("0"));
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        return callManagerMapper.selectByExample(callManagerExample);
    }

}
